# encoding: utf-8
from PyQt4 import uic, QtGui, QtCore
import chords
import midi

form, GuiType = uic.loadUiType(__file__[:-2]+'ui')

class Bat7Window(GuiType, form):
    def __init__(self):
        GuiType.__init__(self)
        self.setupUi(self)
        self.currentChord = None
        self.currentChordFirstNote = None
        self.channel = midi.Channel()
        self.notes_playing = []
        self.timer = None
        self.arpeggio = None
        
        self.nextButton.pressed.connect(self.nextClicked)
        self.nextButton.released.connect(self.stopPlaying)
        self.showAnswerButton.clicked.connect(self.showAnswer)
        self.againButton.pressed.connect(self.startPlayingChord)
        self.againButton.released.connect(self.stopPlaying)
        self.arpeggioButton.clicked.connect(self.arpeggioClicked)
        
        
    def getNextChord(self):
        import random
        chord = random.choice(chords.everything)
        chord = chord.getInversion()
        return chord
    
    def nextClicked(self):
        self.stopPlaying()
        self.answerLabel.setText('')
        self.currentChord = self.getNextChord()
        self.currentChordFirstNote = self.currentChord.getStartingNote()
        self.startPlayingChord()
    
    def startPlayingNote(self, note):
        self.channel.startnote(note)
        self.notes_playing.append(note)
    
    def stopPlayingNote(self, note):
        self.channel.stopnote(note)
        self.notes_playing.remove(note)
        
    def startPlayingChord(self):
        x = self.currentChordFirstNote
        self.startPlayingNote(x)
        for interval in self.currentChord.intervals:
            x += interval
            self.startPlayingNote(x)

    def stopPlaying(self):
        for x in self.notes_playing:
            self.channel.stopnote(x)
        self.notes_playing = []
        
        if self.arpeggio != None:
            self.timer.stop()
            self.timer = None
            self.arpeggio = None
        
    def showAnswer(self):
        self.answerLabel.setText(self.currentChord.name)
        print self.answerLabel.text(), self.currentChordFirstNote
        
    def arpeggioPlayer(self):
        x = self.currentChordFirstNote
        self.startPlayingNote(x)
        yield
        for i in self.currentChord.intervals:
            self.stopPlayingNote(x)
            x += i
            self.startPlayingNote(x)
            yield
        self.stopPlaying()
    
    def arpeggioTimerTimeout(self):
        try:
            self.arpeggio.next()
        except StopIteration:
            pass
        
    def arpeggioClicked(self):
        if self.arpeggio != None:
            return
        self.arpeggio = self.arpeggioPlayer()
        self.timer = QtCore.QTimer(self)
        self.timer.setInterval(500)
        self.timer.timeout.connect(self.arpeggioTimerTimeout)
        self.timer.start()
        self.arpeggioTimerTimeout()
        



if __name__ == '__main__':
    app = QtGui.QApplication([])
    w = Bat7Window()
    w.show()
    
    app.exec_()